import json
from datetime import datetime
from zhipuai import ZhipuAI


client = ZhipuAI(api_key="")  # 请填写您自己的APIKey


messages = [
    {"role": "user", "content": "你好,你可以做什么?(所有的回复都简洁的回答)"},
]
while True:
    response = client.chat.completions.create(
        model="glm-4-plus",  # 请填写您要调用的模型名称
        messages=messages,
    )
    assistant = response.choices[0].message.content
    print(assistant)

    user_input = input("请输入你的消息（输入'exit'退出）：")
    if user_input.lower() == 'exit':
        break

    messages.append({"role": "assistant", "content": f"{assistant}"})
    messages.append({"role": "user", "content": f"{user_input}"})


# 保存聊天记录到JSON文件
date_str = datetime.now().strftime('%Y-%m-%d')
filename = f'记录-{date_str}.json'
with open(filename, 'w', encoding='utf-8') as f:
    json.dump(messages, f, ensure_ascii=False, indent=4)

print(f"聊天记录已保存到文件：{filename}")